package com.sprouting.ops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sprouting.ops.annotations.invoke.IdempotentSingle;
import com.sprouting.ops.interfaces.AddGroup;
import com.sprouting.ops.interfaces.ConstantBase;
import com.sprouting.ops.interfaces.UpdGroup;
import com.sprouting.ops.model.DromaraFileStorage;
import com.sprouting.ops.model.OpsThirdPartyUser;
import com.sprouting.ops.model.OpsTool;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.qo.OpsThirdPartyUserQo;
import com.sprouting.ops.service.DromaraFileStorageService;
import com.sprouting.ops.service.OpsThirdPartyUserService;
import com.sprouting.ops.utils.CheckTool;
import com.sprouting.ops.utils.EntityTool;
import com.sprouting.ops.utils.IdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 厂商人员(OpsThirdPartyUser)(OPS_THIRD_PARTY_USER) 表控制层
 *
 * @author ：lx（长沙麓谷）
 * @date 创建时间：2025-07-31 21:53:11
 */
@Slf4j
@RestController
@RequestMapping("opsThirdPartyUser")
public class OpsThirdPartyUserController {

    @Resource
    private OpsThirdPartyUserService opsThirdPartyUserService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;



    /**
     * 分页查询厂商人员
     * @param query 查询参数
     * @return
     */
    @PostMapping("getListPage")
    public ResultJson getListPage(OpsThirdPartyUserQo query){
        Page page = query.getPage();
        return ResultJson.success(opsThirdPartyUserService.getListPage(page, query));
    }

    /**
     * 删除厂商人员
     * @param id 厂商人员id
     * @return
     */
    @PostMapping("delOpsThirdPartyUser")
    @IdempotentSingle
    public ResultJson delOpsThirdPartyUser(String id){
        if (CheckTool.checkNull(id)){
            return ResultJson.errorCheck();
        }

        OpsThirdPartyUser partyUser = opsThirdPartyUserService.getById(id);
        if (partyUser == null){
            return ResultJson.success();
        }

        DromaraFileStorage opsUploadFile = dromaraFileStorageService.delFileBefore(partyUser.getIco());

        opsThirdPartyUserService.delOpsThirdPartyUser(partyUser, opsUploadFile);
        return ResultJson.success();
    }

    /**
     * 更新厂商人员
     * @param bo 修改参数
     * @return
     */
    @PostMapping("updOpsThirdPartyUser")
    @IdempotentSingle
    public ResultJson updOpsThirdPartyUser(@Validated(UpdGroup.class) OpsThirdPartyUser bo){
        String msg = checkOpsThirdPartyUser(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        OpsThirdPartyUser oldDb = opsThirdPartyUserService.getById(bo.getId());
        DromaraFileStorage[] uploadFileArray = dromaraFileStorageService.updFileBefore(bo.getIco(), oldDb.getIco(), bo.getId(), ConstantBase.FileSourceType.THIRD_PARTY_USER_ICO);

        EntityTool.updBase(bo);
        opsThirdPartyUserService.updOpsThirdPartyUser(bo, uploadFileArray);
        return ResultJson.success(bo);
    }

    /**
     * 保存厂商人员
     * @param bo 保存参数
     * @return
     */
    @PostMapping("saveOpsThirdPartyUser")
    @IdempotentSingle
    public ResultJson saveOpsThirdPartyUser(@Validated(AddGroup.class) OpsThirdPartyUser bo){
        String msg = checkOpsThirdPartyUser(bo);
        if (msg != null){
            return ResultJson.error(msg);
        }

        bo.setId(IdTool.getId());
        EntityTool.saveBase(bo);

        // 处理文件
        DromaraFileStorage unifiedFileInfo = dromaraFileStorageService.toProductionBefore(bo.getIco(), bo.getId(), ConstantBase.FileSourceType.THIRD_PARTY_USER_ICO);

        opsThirdPartyUserService.saveOpsThirdPartyUser(bo, unifiedFileInfo);
        return ResultJson.success(bo);
    }


    /**
     * 厂商人员数据校验
     * @param bo 厂商人员 校验数据
     * @return 非null则有错误
     */
    private String checkOpsThirdPartyUser(OpsThirdPartyUser bo){
        if (bo.getSex() == null){
            bo.setSex(ConstantBase.Sex.UNKNOWN);
        }
        return null;
    }

}
